<?php

/**
 * Description of Critere
 *
 * @author Elodie
 * @version 1.0
 */
class CritereEnseignant
{
    
    /**
     * @var int
     */
    protected $IdCritere;
    
    /**
     * @var int
     */
    protected $IdEnseignant;

    /**
     * @var int
     */
    protected $IdTypeCritere;

    /**
     * @var string
     */
    protected $ValueCritere;

    /**
     * @var bool
     */
    public $requeteOk;

    /**
     * @var string
     */
    public $errormsg;

    /**
     *
     * @param int $id
     */
    public function __construct($id = 0)
    {

        if (empty($id))
        {

            $this->IdCritere = 0;
            $this->IdEnseignant = 0;
            $this->IdTypeCritere = 0;

            $this->ValueCritere = "";

            $this->requeteOk = true;
            $this->errormsg = "";
        }
        else
        {

            $sql = SQL::getInstance()->getConnection();

            //On retourne l'entree demandee
            $query = 'SELECT * FROM CritereEnseignant WHERE idCritere = :id';
            $c = $sql->prepare($query);
            $c->bindParam(':id', $id, PDO::PARAM_INT);

            //Execution de la requête retourne false si echec
            if ($c->execute())
            {

                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {

                    $this->IdCritere = $result->IdCritere;
                    $this->IdEnseignant = $result->IdEnseignant;
                    $this->IdTypeCritere = $result->IdTypeCritere;

                    $this->ValueCritere = $result->ValueCritere;

                    $this->requeteOk = true;
                    $this->errormsg = "Critere trouve";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Ce critere n'existe pas !";
                }
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    /**
     * Insert un nouveau Critere pour un ensiegnant dans la base
     */
    public function insertCritereEnseignant()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'INSERT INTO CritereEnseignant
		(
			 IdEnseignant,
			 IdTypeCritere,
			 ValueCritere
		)
		VALUES
		(
			:idEnseignant,
			:idTypeCritere,
			:valueCritere
		)';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':idEnseignant', $this->IdEnseignant, PDO::PARAM_INT);
            $c->bindParam(':idTypeCritere', $this->IdTypeCritere, PDO::PARAM_INT);

            $c->bindParam(':ValueCritere', $this->ValueCritere, PDO::PARAM_STR);

            if ($c->execute())
            {
                $this->IdCritere = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Insertion reussie !";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdCritere = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->IdCritere = 0;
        }
    }

    /**
     * Modification d'un critere enseignant
     */
    public function updateCritereEnseignant()
    {
        $sql = SQL::getInstance()->getConnection();

        /* Je ne sais pas ce que je modifie normalement on doit pas pouvoir
         * modifier nom de l'enseignant et le type, uniquement la reponse, mais
         * dans ce cas faut pas que ce soient des cle primaires ?
         */

        $query =
                'UPDATE QuestionEvaluation SET
			 ValueCritere = :ValueCritere
		WHERE
			IdCritere = :id
		';

        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':ValueCritere', $this->ValueCritere, PDO::PARAM_STR);

            if ($c->execute())
            {
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Modification reussie!";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
    }

    /**
     * Suppression d'un critère enseignant
     */
    public function deleteCritereEnseignant()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = 'DELETE FROM CritereEnseignant WHERE IdCritere = :id';
        $c = $sql->prepare($query);
        $c->bindParam(':id', $this->IdCritere, PDO::PARAM_INT);
        $c->execute();
    }

//************** Getter et Setter **********************
    /**
     *
     * @return int
     */
    public function getIdCritere()
    {
        return $this->IdCritere;
    }

    /**
     *
     * @return int
     */
    public function getIdEnseignant()
    {
        return $this->IdEnseignant;
    }

    /**
     *
     * @return int
     */
    public function getIdTypeCritere()
    {
        return $this->IdTypeCritere;
    }

    /**
     *
     * @return string
     */
    public function getValueCritere()
    {
        return $this->ValueCritere;
    }

    /**
     *
     * @param string $value
     * @return mixed
     */
    public function setValueCritere($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value != "")
        {
            $this->ValueCritere = htmlentities($value);
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La valeur ne peut etre null !";
            }
            if (isset($value) && $value == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "La valeur ne peut etre vide !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cette valeur n'est pas valide !";
            }

            $this->ValueCritere = "";
        }

        return $errTab;
    }

    /**
     *
     * @param int $value
     * @return mixed
     */
    public function setIdEnseignant($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdEnseignant = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdEnseignant ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdEnseignant ne peut etre <= 0 !";
            }

            $this->IdEnseignant = 0;
        }
        return $errTab;
    }

    /**
     *
     * @param int $value
     * @return mixed
     */
    public function setIdTypeCritere($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdTypeCritere = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdTypeCritere ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdTypeCritere ne peut etre <= 0 !";
            }

            $this->IdTypeCritere = 0;
        }
        return $errTab;
    }

    /**
     *Renvoi la liste des IdCritere qui ont pour IdEnseignant celui passé en paramètre
     * @param <type> $idEnseignant
     * @return <type> list IdCritere
     */
    public static function getListIdCritereByIdEnseignant($idEnseignant){
        if (isset($idEnseignant) && !empty($idEnseignant))
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT IdCritere
                          FROM CritereEnseignantRS
                          WHERE IdEnseignant = :id";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $idEnseignant, PDO::PARAM_INT);
            $c->execute();
            $result = $c->fetchAll(PDO::FETCH_OBJ);
            return $result;
        }
    }

}

?>